package com.teamone.etl.whitelist.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.enterprise.entity.Enterprise;
import com.teamone.etl.whitelist.entity.WhiteList;
import com.teamone.etl.whitelist.feign.WhiteListFeginService;
import com.teamone.etl.whitelist.service.WhiteListService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * IP白名单(WhiteList)表控制层
 *
 * @author makejava
 * @since 2021-01-20 11:07:18
 */
@RestController
@RequestMapping("whiteList")
public class WhiteListController extends ApiController {
    public static final int ZERO = 0;
    /**
     * 服务对象
     */
    @Resource
    private WhiteListService whiteListService;

    @Resource
    private WhiteListFeginService whiteListFeginService;

    /**
     * 分页管理列表
     * @param page 当前页
     * @param size 每页显示条数
     * @return 白名单列表
     */
    @GetMapping("/adminWhiteList")
    public R adminWhiteList(@RequestParam(value = "page" ,defaultValue = "1")Integer page,
                            @RequestParam(value = "size" ,defaultValue = "10")Integer size){

      PageInfo<WhiteList> adminSearch =whiteListService.adminWhiteList(page,size);
      return success(adminSearch);
    }

    /**
     *  已启用白名单
     * @param page 当前页
     * @param size 每页显示条数
     * @return 是否成功
     */
    @GetMapping("/userWhiteList")
    public R userWhiteList(@RequestParam(value = "page" ,defaultValue = "1")Integer page,
                           @RequestParam(value = "size" ,defaultValue = "10")Integer size){
        return success(whiteListService.userWhiteList(page,size));
    }

    /**
     * 根据id删除IP白名单
     * @param id  白名单id
     * @return 是否成功
     */
    @DeleteMapping("/deleteWhiteList/{id}")
    public R deleteWhiteList(@PathVariable("id")Integer id){
        return success(whiteListService.deleteWhiteList(id)>ZERO);
    }

    /**
     * 添加IP白名单
     * @param whiteList 前台传入对象
     * @return 是否成功
     */
    @PostMapping("/addWhiteList")
    public R addWhiteList(@RequestBody WhiteList whiteList){
        return success(whiteListService.addWhiteList(whiteList)>ZERO);
    }

    /**
     * 修改白名单
     * @param whiteList 修改内容
     * @return 是否成功
     */
    @PutMapping("/updateWhiteList")
    public R updateWhiteList(@RequestBody WhiteList whiteList){
        return success(whiteListService.updateWhiteList(whiteList)>ZERO);
    }

    /**
     * 获取公司列表
     * @return 是否成功
     */
    @GetMapping("/getCompany")
    public R<List<Enterprise>> getCompany(){
        return success(whiteListFeginService.getAll());
    }

    /**
     * 启用IP or 禁用IP
     * @param id 地址id
     * @return 是否成功
     */
    @PutMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable("id") Integer id){
        return success(whiteListService.updateStatus(id)>ZERO);
    }

    /**
     * 修改IP地址
     * @param whiteList 修改内容
     * @return 是否成功
     */
    @PutMapping("/updateIp")
    public R updateIp(@RequestBody WhiteList whiteList){
        return success(whiteListService.updateIp(whiteList)>ZERO);
    }

    /**
     * 添加多个IP地址
     * @param ipAddressMulti 多个ip地址
     * @return 是否成功
     */
    @PostMapping("/addIpAddress")
    public R addIpAddress(@RequestParam("ipAddressMulti") String ipAddressMulti){
        return success(whiteListService.addIpAddress(ipAddressMulti)>ZERO);
    }

}